const Service = require('../../../core/service/ApiService');

class AdminService extends Service {

    //登录
    async login(account, pwd, remember) {
        const dao = this.service.admin.admin.dao;
        const admin = await dao.findAdminByAccount(account);
        if (!admin)
            throw new this.error.BusinessError('该账号不存在');
        const encryptedPwd = this.helper.sha1(pwd);
        if (encryptedPwd !== admin.pwd)
            throw new this.error.BusinessError('密码错误');
        this.ctx.session.account = admin.account;
        if (remember) {
            this.ctx.session.maxAge = 604800000;  //7天
        }
    }

    //教工登录
    async staffLogin(staffInfo) {
        const dao = this.service.admin.admin.dao;
        const staInfo = await dao.findStaffInfoByNumber(staffInfo.staff_number);
        if(!staInfo)
            throw new this.error.BusinessError('该账号不存在');
        const enPwd = this.helper.sha1(staffInfo.staff_password);
        if(enPwd!==staInfo.staff_password)
            throw new this.error.BusinessError('密码错误');

    }

    //退出登录
    async logout() {
        this.ctx.session = null;
    }

}

module.exports = AdminService;
